package com.gcl.rabbitmq.utils.com.gcl.rabbitmq;

import com.gcl.rabbitmq.utils.RabbitMQUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class SingerMessage {

    public static final String QUEUE_NAME = "Confirm";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {

        Channel channel = RabbitMQUtil.getChannel();

        // 开启发布确认
        channel.confirmSelect();

        // 声明队列
        channel.queueDeclare(QUEUE_NAME,true,false,false,null);

        // 绑定路由

        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNext()){
            String msg = scanner.next();
            channel.basicPublish("",QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN,msg.getBytes(StandardCharsets.UTF_8));

            boolean flag = channel.waitForConfirms();

            if (flag){
                System.out.println("消息发布确认成功！");
            }

        }


    }

}
